package com.hsg.common.otherlogin.ui.login

import android.content.Intent
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.hsg.common.otherlogin.R
import com.hsg.common.otherlogin.SignatureUtil
import com.hsg.common.otherlogin.databinding.LoginFragmentBinding
import com.hsg.common.thirdloginutil.bean.ThirdLoginRespond
import com.hsg.common.thirdloginutil.login.fb.FacebookLogin
import com.hsg.common.thirdloginutil.login.google.GoogleLogin
import com.hsg.common.thirdloginutil.login.line.LineLogin
import com.hsg.common.thirdloginutil.login.twitter.TwitterLogin

class LoginFragment : Fragment() {

    companion object {
        fun newInstance() = LoginFragment()
    }

    private lateinit var viewModel: LoginViewModel

    private var binding: LoginFragmentBinding? = null

    private var facebookLogin : FacebookLogin? = null
    private var twitterLogin : TwitterLogin? = null
    private var lineLogin : LineLogin? = null
    private var googleLogin : GoogleLogin? = null

    private lateinit var activity: AppCompatActivity

    private val TAG = "LoginFragment"

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = LoginFragmentBinding.inflate(layoutInflater)
        return binding!!.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
        activity = requireActivity() as AppCompatActivity

        // TODO: Use the ViewModel
        val sha1 = SignatureUtil.getSignatureSHA1WithColon(requireContext(), requireActivity().packageName)
        val hash = SignatureUtil.getHashKey(requireContext(), requireActivity().packageName)

        googleLogin = GoogleLogin(this,
            googleServerClientId = "259287201199-s854ief4lsb0kkjl3kgnnck22pisbe3n.apps.googleusercontent.com",
            filterByAuthorizedAccounts = false) { respond: ThirdLoginRespond? ->
            Log.i(TAG, "google respond: $respond")
            Toast.makeText(requireActivity(), respond.toString(), Toast.LENGTH_SHORT).show()
        }

        lineLogin = LineLogin(this, "1657543992") { respond: ThirdLoginRespond? ->
            Log.i(TAG, "line respond: $respond")
            Toast.makeText(requireActivity(), respond.toString(), Toast.LENGTH_SHORT).show()
        }

        binding?.fb?.setOnClickListener {
            facebookLogin = FacebookLogin(this) { respond: ThirdLoginRespond? ->
                Log.i(TAG, "facebook respond: $respond")
                Toast.makeText(requireActivity(), respond.toString(), Toast.LENGTH_SHORT).show()
            }
            facebookLogin?.startLogin()
        }

        binding?.twitter?.setOnClickListener {
            twitterLogin = TwitterLogin(activity) { respond: ThirdLoginRespond? ->
                Log.i(TAG, "twitter respond: $respond")
                Toast.makeText(requireActivity(), respond.toString(), Toast.LENGTH_SHORT).show()
            }
            twitterLogin?.startLogin()
        }

        binding?.line?.setOnClickListener {
            lineLogin?.startLogin()
        }

        binding?.google?.setOnClickListener {
            googleLogin?.startLogin()
        }

        binding?.signature?.setOnClickListener {
            Log.i(TAG, "sha1 : $sha1 \n hash : $hash")
        }

        binding?.signOut?.setOnClickListener {
            facebookLogin?.signOut()
            googleLogin?.signOut()
            twitterLogin?.signOut()
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
//        facebookLogin?.handleLoginResult(requestCode, resultCode, data)
    }

}